/**
*      author: Y_Yao
*      created: 2024-12-06 20:50:55
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 7;
int n,m;
vector<vector<int>> v(N);
int ans = INF;
bool vis[N];
int flag;

int bfs()
{
    int ans = INF;
    queue<pair<int,int>> q;
    q.push({1,0});
    while(!q.empty())
    {
        auto t = q.front();
        q.pop();
        int step = t.second;
        int cur = t.first;
        for(auto e : v[cur])
        {
            if(e == 1)
            {
                ans = min(ans,step + 1);
            }
            if(vis[e]) continue;
            vis[e] = true;
            q.push({e,step + 1});
        }
    }
    if(ans == INF)
    {
        return -1;
    }
    return ans;
}

void solve()
{
    cin >> n >> m;
    for(int i = 1;i <= m;i++)
    {
        int a,b;
        cin >> a >> b;
        v[a].push_back(b);//a to b
    }
    cout << bfs() << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}